package com.shengqi.xinnongren.work.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.DividerItemDecoration;

import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.common.md5.SafeUtils;
import com.shengqi.xinnongren.common.utils.LogUtil;
import com.shengqi.xinnongren.common.widgets.CustomDialog;
import com.shengqi.xinnongren.work.dialog.AddToCartDialog;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.home.activities.MainActivity;
import com.shengqi.xinnongren.work.ui.user.activities.LoginActivity;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by LuoYaWei on 2018/7/26.
 */
public class Global {

    /**
     * 生成RecyclerView 分割线
     *
     * @param activity
     * @param dpHeight
     * @param color
     * @return
     */
    public static DividerItemDecoration genRecyclerViewItemDecoration(Activity activity, int dpHeight, int color) {
        DividerItemDecoration itemDecoration = new DividerItemDecoration(activity, DividerItemDecoration.VERTICAL);
        Point outSize = new Point();
        activity.getWindow().getWindowManager().getDefaultDisplay().getSize(outSize);
        Bitmap bitmap = Bitmap.createBitmap(outSize.x, (int) (dpHeight * activity.getResources().getDisplayMetrics().density), Bitmap.Config.ARGB_8888);
        new Canvas(bitmap).drawColor(color);
        itemDecoration.setDrawable(new BitmapDrawable(bitmap));
        return itemDecoration;
    }

    /**
     * 检查是否已登录
     *
     * @param context
     * @return true 为已登录
     */
    public static boolean checkLogin(Context context) {
        if (!UserManager.getInstance().isLogin()) {
            context.startActivity(new Intent(context, LoginActivity.class));
            return false;
        }
        return true;
    }

    public static HttpRespond<String> getHttpResult(String json) {
        StackTraceElement stackTraceElement = new Throwable().getStackTrace()[2];
        LogUtil.e(String.format("返回数据：%s.%s()", stackTraceElement.getClassName(), stackTraceElement.getMethodName()), json);
        HttpRespond<String> httpRespond = new HttpRespond<>();
        try {
            JSONObject resultObj = new JSONObject(json);
            httpRespond.message = resultObj.optString("message");
            httpRespond.result = resultObj.optInt("result");
            if (resultObj.optInt("isaes") == 1)
                httpRespond.data = SafeUtils.decrypt(resultObj.optString("data"));
            else
                httpRespond.data = resultObj.optString("data");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return httpRespond;
    }

    /**
     * 规格dialog
     *
     * @param id
     */
    public static void showSpecificationDialog(Context context, String id) {
        if (Global.checkLogin(context))
            new AddToCartDialog.Builder(context).setGoodsId(id).build().show();
    }

    /**
     * 通知MainActivity更新购物车数字
     */
    public static void notifyCartNumChanged(Context context) {
        LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(MainActivity.ACTION_REFRESH_CART_NUM));
    }

    public static void reLogin(Context context) {
        new CustomDialog.Builder(context)
                .setTitle("重新登录")
                .setContent("登录已过期，请重新登录！")
                .setShowCancel(false)
                .setListener(new CustomDialog.OnButtonClickListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        dialog.dismiss();
                        context.startActivity(new Intent(context, LoginActivity.class));
                    }

                    @Override
                    public void onCancel(Dialog dialog) {
                        dialog.dismiss();
                    }
                }).build().show();
    }

    public static void exitApp(Context context, String message) {
        new CustomDialog.Builder(context)
                .setTitle("提示")
                .setContent(message)
                .setShowCancel(false)
                .setListener(new CustomDialog.OnButtonClickListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        dialog.dismiss();
                        System.exit(0);
                    }

                    @Override
                    public void onCancel(Dialog dialog) {
                        dialog.dismiss();
                    }
                }).build().show();
    }

    public static void main(String[] args) {
        String result = "success=true&result_code=200&app_id=2018090561232445&auth_code=3585372719ee41819e4ddce142c7ZX81&scope=kuaijie&alipay_open_id=20880005246861801861831681715681&user_id=2088022394111814";
        int userIndex = result.indexOf("auth_code") + 10;
        System.out.println(userIndex);
        String autonCode = result.substring(result.indexOf("auth_code") + 10, result.indexOf("auth_code") + 10 + 32);
        System.out.println(autonCode);
        String user_id = result.substring(result.indexOf("user_id") + 8, result.indexOf("user_id") + 8 + 16);
        System.out.println(user_id);
    }
}
